﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.Core.Utils;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.BLL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Data;
namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 火灾信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FireInfoController : ControllerBase
    {
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> Add(FireInfoRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                if (obj != null
                        && SundryHelper.IsNumber(obj.AddressCounty)
                        && SundryHelper.IsNumber(obj.AddressVillage)
                        && SundryHelper.IsNumber(obj.AddressTown)
                        && !string.IsNullOrEmpty(obj.FireName)
                        && !string.IsNullOrEmpty(obj.AlarmContent)
                        && obj.HappenDatetime != null
                        && obj.AlarmTime != null)
                {
                    string ip = HttpContext.GetClientIp();
                    Fireinfo fireInfo = new Fireinfo();

                    #region 火灾主体
                    fireInfo.FireName = obj.FireName;
                    fireInfo.HappenDatetime = obj.HappenDatetime;
                    fireInfo.AlarmTime = obj.AlarmTime;
                    fireInfo.AlarmContent = obj.AlarmContent;
                    fireInfo.Alarm = obj.Alarm;
                    fireInfo.AddressCounty = obj.AddressCounty;
                    fireInfo.AddressTown = obj.AddressTown;
                    fireInfo.AddressVillage = obj.AddressVillage;
                    List<Linchpin> temp = LinchpinBLL.Instance.GetLinchpin(new List<long>() { 
                                                                                obj.AddressCounty.ToLong(), 
                                                                                obj.AddressTown.ToLong(), 
                                                                                obj.AddressVillage.ToLong() });
                    string address = string.Empty;
                    address += temp.Where(p => p.LinchpinId == obj.AddressCounty.ToLong()).FirstOrDefault().LinchpinName;
                    address += temp.Where(p => p.LinchpinId == obj.AddressTown.ToLong()).FirstOrDefault().LinchpinName;
                    address += temp.Where(p => p.LinchpinId == obj.AddressVillage.ToLong()).FirstOrDefault().LinchpinName;
                    fireInfo.Address = address;
                    fireInfo.ReporterName = obj.ReporterName;
                    fireInfo.ReporterUnit = obj.ReporterUnit;
                    fireInfo.ReporterWorknum = obj.ReporterWorknum;
                    fireInfo.ReporterPhone = obj.ReporterPhone;
                    fireInfo.UserId = obj.UserID;
                    fireInfo.Ip = ip;
                    fireInfo.IsChange = 0;
                    fireInfo.IsState = obj.IsState;
                    fireInfo.TransDatetime = DateTime.Now;
                    fireInfo.IsSupplementary = obj.IsSupplementary;
                    #endregion

                    List<Smsinfo> smsInfos = new List<Smsinfo>();

                    #region 短信息主体
                    
                    //if (obj.SMSs != null && obj.SMSs.Count > 0)
                    //{
                    //    foreach (var item in obj.SMSs)
                    //    {
                    //        Smsinfo model = new Smsinfo();
                    //        model.UserId = fireInfo.UserId;
                    //        model.SmsContent = item.Content;
                    //        model.MobileCode = item.UserPhone;
                    //        model.Receiver = item.UserName;
                    //        smsInfos.Add(model);
                    //    }
                    //}
                    #endregion 

                    List<Firesite> fireSites = new List<Firesite>();

                    #region 定位火点主体
                    if (obj.FireSites != null && obj.FireSites.Count > 0)
                    {
                        foreach (var item in obj.FireSites)
                        {
                            Firesite model = new Firesite();
                            model.Latitude = item.Latitude;
                            model.Longitude = item.Longitude;
                            model.Altitude = item.Altitude;
                            model.Content = string.Empty;
                            model.TransDatetime = fireInfo.TransDatetime;
                            model.UserId = obj.UserID;
                            model.Ip = ip;
                            fireSites.Add(model);
                        }
                    }
                    #endregion

                    var data= FireInfoBLL.Instance.Add(fireInfo, fireSites, smsInfos);

                    //发送MQTT
                    ControllerHelper.SendMessage(data.FirstParam);

                    //返回火灾Id
                    result.FirstParam = data.FirstParam.FirstOrDefault()?.FireId;
                }
                else
                {
                    result.Status = 0;
                    result.Message = "Missing Required Parameters";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireInfo/Add Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic, dynamic> GetFireInfoByPage(FireInfoSearchRequest obj) 
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                var page = Pickout.SetPage(obj);
                if (page.StartTime != null)
                {
                    page.StartTime = SundryHelper.StartTimeConvert(page.StartTime.Value);
                }
                if (page.EndTime != null)
                {
                    page.EndTime = SundryHelper.EndTimeConvert(page.EndTime.Value);
                }
                return BLL.FireInfoBLL.Instance.GetFireInfoByPage(obj.IsState, obj.AddressCounty, obj.AddressTowns, page);
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireInfo/GetFireInfoByPage Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }

        /// <summary>
        /// 火灾详情
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> GetFireInfoDetail(RecordSearchRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                return BLL.FireInfoBLL.Instance.GetFireInfoDetail(obj.FireId,obj.Types);
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireInfo/GetFireInfoDetail Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }
    }
}